const fs = require('fs');
const path = require('path')
const WriteStream = require('./writeStream')

const ws = new WriteStream(path.resolve(__dirname, 'write.md'), { //  fs 继承了 Stream上的createReadStream方法
    // const ws = fs.createWriteStream(path.resolve(__dirname, 'write.md'), { //  fs 继承了 Stream上的createReadStream方法
    flags: 'w',//  r 供fs.open使用
    encoding: null, // 默认读出为buffer类型
    mode: 0o666,
    autoClose: true, // 是否触发 close事件 
    emitClose: true,
    start: 0, // 读取开始位置
    highWaterMark: 2, // 单次读取大小
})

// let flag = ws.write('1', 'utf8',function (...args) {
//     console.log(...args)
// })

// console.log(flag) // true

// flag = ws.write('2', 'utf8', function (...args) {
//     console.log(...args)
// })
// console.log(flag)// true
// console.log(ws.write('1'))
// console.log(ws.write('2'))
// console.log(ws.write('3'))
let index = 1;
function write() {
    let flag = true;
    while (index !== 10 && flag) {
        flag = ws.write(index++ + '') // 只接受 string | buffer
    }
}
write()

ws.on('drain', function () {
    write()
    console.log('drain')
})

setTimeout(() => {
    ws.end('结束')
}, 1000)